package com.jiashihui.schema_lgorithm.spring;

import com.jiashihui.schema_lgorithm.spring.bean.User;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @author jiashihui
 * @describe: 创建BeanFactory的三种方式（XmlBeanFactory、ClassPathApplicationContext、FileSystemXmlApplicationContext）
 * @date 2019/03/20
 */
public class CreateBeanFactory {

    // 方式一：使用XmlBeanFactory获取BeanFactory工厂类对象(已过时，不使用）
    @Test
    public void getUser1() throws Exception{
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring/applicationContext.xml"));
        Class<? extends BeanFactory> class1 = factory.getClass();
        System.out.println(class1);
    }

    // 方式二：使用ClassPathApplicationContext获取容器类(创建容器类对象主要使用的方法)
    @Test
    public void getUser2() throws Exception{
        BeanFactory factory = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        System.out.println(factory.getClass());
    }

    // 方式三：使用FileSystemXmlApplicationContext来获取容器对象
    // 这种方式在创建对象的时候需要传入配置文件的绝对路径，这个方法可以使用项目外部的配置文件
    @Test
    public void getUser3() throws Exception{
        BeanFactory factory = new FileSystemXmlApplicationContext("E:\\IdeaProject\\microservice\\schema_lgorithm\\src\\main\\resources\\spring\\applicationContext.xml");
        System.out.println(factory.getClass());
        User user = (User)factory.getBean("user");
        System.out.println(user.toString());
    }


}
